/*
 * This file is part of b.AMP.
 *
 * b.AMP is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * b.AMP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with b.AMP.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef __BAMP_DYNAMIC_LIB_HPP__
#define __BAMP_DYNAMIC_LIB_HPP__

#include <bamp/noncopyable.hpp>
#include <boost/filesystem.hpp>
#include <boost/shared_ptr.hpp>
#include <memory>
#include <string>

namespace bamp
{

    class DynamicLib;
    typedef boost::shared_ptr<DynamicLib> DynamicLibPtr;

    /**
     * DynamicLib is a portable dynamic library loader. It wraps
     * library loading and unloading, as well as symbol resolution,
     * into an OS-agnostic, RAII object.
     */
    class DynamicLib : bamp::Noncopyable
    {

    public:

        /**
         * Constructor. Loads a library immediately.
         * @param path path, on the filesystem, to the library (.so,
         * .dll, ...).
         */
        DynamicLib(const boost::filesystem::path& path);

        /**
         * Destructor. Releases the instance of the library that was
         * aquired during construction.
         */
        ~DynamicLib();

        /**
         * Resolve a symbol
         * @param name symbol name
         * @param sym  a typed pointer to the symbol resolved
         */
        template<typename T>
        T resolve(const std::string& name)
        {
            return (T)_resolve(name);
        }


    private:

        void* _resolve(const std::string& name);

        struct Implementation;
        std::auto_ptr<Implementation> pimpl;
    };

}; // namespace bamp

#endif // __BAMP_DYNAMIC_LIB_HPP__
